using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.ObjectModel;
using System.Linq;

namespace WinUITest.Pages
{
    [Menu("测试页面")]
    public sealed partial class TestPage : Page
    {
        public TestPageViewModel ViewModel { get; } = new();
        public TestPage()
        {
            InitializeComponent();
            Loaded += (s, e) => ViewModel.Init();
        }
    }
    public partial class TestPageViewModel : ObservableObject
    {
        public ObservableCollection<KeyValueViewModel> Items1 { get; } = [
            new(){ Value = "1", Children= [ new(){ Value = "1-1", Children = [new() { Value = "1-1-1" }, new() { Value = "1-1-2" }] }, new(){ Value = "1-2", Children = [new() { Value = "1-2-1" }, new() { Value = "1-2-2" }] }] },
            new(){ Value = "2", Children= [ new(){ Value = "2-1", Children = [new() { Value = "2-1-1" }, new() { Value = "2-1-2" }] }, new(){ Value = "2-2", Children = [new() { Value = "2-2-1" }, new() { Value = "2-2-2" }] }] },
            new(){ Value = "3", Children= [ new(){ Value = "3-1", Children = [new() { Value = "3-1-1" }, new() { Value = "3-1-2" }] }, new(){ Value = "3-2", Children = [new() { Value = "3-2-1" }, new() { Value = "3-2-2" }] }] }
        ];
        [ObservableProperty]
        public partial KeyValueViewModel? SelecctedItem1 { get; set; }
        partial void OnSelecctedItem1Changed(KeyValueViewModel? value)
        {
            Items2.Clear();
            if (value is null)
            {
                Items3.Clear();
            }
            else
            {
                foreach (KeyValueViewModel item in value.Children)
                {
                    Items2.Add(item);
                }
            }
            SelecctedItem2 = Items2.FirstOrDefault();
        }
        public ObservableCollection<KeyValueViewModel> Items2 { get; } = [];
        [ObservableProperty]
        public partial KeyValueViewModel? SelecctedItem2 { get; set; }
        partial void OnSelecctedItem2Changed(KeyValueViewModel? value)
        {
            Items3.Clear();
            if (value is not null)
            {
                foreach (KeyValueViewModel item in value.Children)
                {
                    Items3.Add(item);
                }
            }
            SelecctedItem3 = Items3.FirstOrDefault();
        }
        public ObservableCollection<KeyValueViewModel> Items3 { get; } = [];
        [ObservableProperty]
        public partial KeyValueViewModel? SelecctedItem3 { get; set; }
        public void Init() => SelecctedItem1 = Items1.FirstOrDefault();
    }
    public partial class KeyValueViewModel : ObservableObject
    {
        [ObservableProperty]
        public partial Guid Key { get; set; }
        [ObservableProperty]
        public partial string Value { get; set; }
        public ObservableCollection<KeyValueViewModel> Children { get; init; } = [];
    }
}
